package demo.spring.cloud.hystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import dmeo.spring.cloud.eureka.provider.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;
import org.springframework.stereotype.Service;



/**
 * Created by lushun.chen on 2017/4/9.
 *
 * @author lushun.chen
 */
@Service
public class HystrixService {

    @Autowired
    private RestTemplate restTemplate;

    public static final Logger logger = LoggerFactory.getLogger(HystrixService.class);

    //@HystrixCommand(fallbackMethod = "fallback")
    public User findById(Long id) {
        return restTemplate.getForObject("http://microservice-provider-user/" + id, User.class);
    }

    public User fallback(Long id) {
        logger.info("异常发生，进入fallback方法，接受参数 id={}", id);
        User user = new User();
        user.setId(1L);
        user.setUsername("周杰");
        user.setAge(18);
        return user;
    }
}
